<?php
/**
 * 体检预约
 *
 * User: kaikaic
 * Date: 2018/11/12
 * Time: 21:25
 */

namespace app\api\controller;

use app\api\service\MecService;
use controller\BasicApi;
use service\ConfigService;
use service\ToolsService;
use think\Db;

class Mec extends BasicApi {

    /**
     * 体检中心列表
     */
    public function index() {

        // 计算距离
        $lat1 = "28.22778";
        $lng1 = "112.93886";

        $lat2 = "28.236279";
        $lng2 = "112.960662";
        $distance = ToolsService::calculateDistance($lat1, $lng1, $lat2, $lng2);

        if ($distance < 1) {
            echo $distance * 1000 . "米";
        }else {
            echo $distance . "公里";
        }
    }
    public function getDistance($lat1,$lng1,$lat2,$lng2) {

        // 计算距离
        $distance = ToolsService::calculateDistance($lat1, $lng1, $lat2, $lng2);
        if ($distance < 1) {
            return $distance * 1000 . "米";
        }else {
            return $distance . "公里";
        }

    }
    public function meclist(){
        $page = $this->request->param('page',1);
        $searchname = $this->request->param('searchname','');
        $sort = $this->request->param('sort',0);
        $pageSize = 6;
        $lng  = $this->request->param('long',0);
        $lat  = $this->request->param('lat',0);
        $category_id  = $this->request->param('category_id',0);
        if($category_id){
            $mecres  = Db::name('exam_mec')->
            where('name','like',"%$searchname%")->
            where('status','1')->
            where('is_team','0')->
            where('category_id',$category_id)->
            order('sort desc')->
            select();
        }else{
            $mecres  = Db::name('exam_mec')->
            where('name','like',"%$searchname%")->
            where('status','1')->
            where('is_team','0')->
            order('sort desc')->
            select();
        }

        if($mecres){
            foreach ($mecres as $key =>&$item){
                $item['distance'] = ToolsService::calculateDistance($item['lat'],$item['lng'],$lat,$lng);
                $item['num'] = Db::name('exam_order')->where('mec_id',$item['id'])->count() + $item['peoplesnum'] ;
                $item['tags'] = explode(',',$item['tags']);
            }
            $res = arr_sort($mecres, 'distance');
            foreach ($res as $key =>&$val){
                if($val['distance']>1){
                    $val['distance'] = round($val['distance'],2)."km";
                }else{
                    $val['distance'] = round($val['distance']*1000,0)."m";
                }
            }
            $mecresz = array_slice($res,$pageSize*($page-1),$pageSize);
            $this->success('获取成功',$mecresz);
        }else{
            $this->error('暂无数据');
        }

    }

    public function mecinfo(){
        $id = $this->request->param('id',0);
        $lng  = $this->request->param('long',0);
        $lat  = $this->request->param('lat',0   );
        if($id){
            //医院详情
            $mecInfo = Db::name('exam_mec')->find($id);
            if($mecInfo){
                $mecInfo['distance'] = self::getDistance($mecInfo['lat'],$mecInfo['lng'],$lat,$lng);
                $mecInfo['num'] = Db::name('exam_order')->where('mec_id',$id)->count()+ $mecInfo['peoplesnum'];
                $mecInfo['tags'] = explode(',',$mecInfo['tags']);
                $mecInfo['settype'] = MecService::getSettype(14);
                //医院套餐
                $mecInfo['setlist'] = MecService::getMecSet($id,0);
                if($mecInfo['photos']){
                    $mecInfo['imgarr'] = explode("|",$mecInfo['photos']);
                }else{
                    $mecInfo['imgarr'] = ['/static/xcximg/yiyuan.png'];
                }
//                if($mecInfo['distance']>1){
//                    $mecInfo['distance'] = round($mecInfo['distance'],2)."km";
//                }else{
//                    $mecInfo['distance'] = round($mecInfo['distance']*1000,0)."m";
//                }
                $this->success('获取成功',$mecInfo);
            }else{
                $this->error('医院不存在');
            }
        }else{
            $this->error("参数错误");
        }
    }
    public function setinfo(){
        $imagearr = ConfigService::getsetImage1();
        $id = $this->request->param('id',0);
        if($id){
            //医院详情
            $setInfo = Db::name('exam_set')->find($id);
            if($setInfo){
                $arr = array();
                $item_ids = unserialize($setInfo['item_ids']);
                for ($i=0;$i<count($item_ids) ;$i++){
                    $deails = Db::name('exam_item e')
                        ->join('exam_item_class ei','e.item_set_id = ei.id')
                        ->field('e.name as ename,e.*,ei.* ')
                        ->where('e.id',$item_ids[$i])->find();

                    if($deails['name']=='默认分类'){
                    }else{
                        $arr[$i] = $deails;
                        $arr[$i]['pname'] = $deails['name'];
                    }

                }
                $setInfo['main_info'] = str_replace('src="','src="'.$this->request->domain(),$setInfo['main_info']);
                $res = array(); //想要的结果
                foreach ($arr as $k => $v) {
                    $res[$v['pname']][] = $v;
                }
                if($setInfo['cover']){
                    $setInfo['set_image']=$setInfo['cover'];
                }else{
                    $setInfoarr = explode(',',$setInfo['set_type']);
                    $setInfo['set_image']=$imagearr[$setInfoarr[0]];
                }


                $setInfo['nameArr'] = $res;
                $setInfo['itemArr'] = $arr;
                $setInfo['mecinfo'] =  Db::name('exam_mec')->find($setInfo['mec_id']);
                $this->success('获取成功',$setInfo);
            }else{
                $this->error('套餐不存在');
            }
        }else{
            $this->error("参数错误");
        }
    }
    public function setlist(){
        $mec_id = $this->request->param('mec_id',0);
        $set_type = $this->request->param('set_type',0);
        $setlist['setlist'] = MecService::getMecSet($mec_id,$set_type);
        $setlist['settype'] = MecService::getSettype(14);
        if($setlist){
            $this->success('获取成功',$setlist);
        }else{
            $this->error("暂无套餐");
        }
    }
    public function highlist(){
        $setlist['setlist'] = MecService::getHighSet();
        $setlist['settype'] = MecService::getSettype(14);
        if($setlist){
            $this->success('获取成功',$setlist);
        }else{
            $this->error("暂无套餐");
        }
    }
}


